package com.hna.eking.AirlineServer.Platform.MapInfo;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hna.eking.AirlineServer.Exception.RunServiceException;
import com.hna.eking.AirlineServer.Mapper.AreaMapInfo;
import com.hna.eking.AirlineServer.Mapper.MapInfo;
import com.hna.eking.AirlineServer.Service.AreaMapService;
import com.hna.eking.AirlineServer.Service.MapInfoService;
import com.hna.eking.AirlineServer.Utils.ResponseSupport;

@RestController
@ControllerAdvice
//@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/mapinfo" )
public class MapInfoController {
	@Autowired
	private MapInfoService mapinfoService;
	@Autowired
	private AreaMapService areaServ;
	
	private Logger logger = LoggerFactory.getLogger(MapInfoController.class);

	@RequestMapping(value = "/mapinfolist", method = { RequestMethod.GET, RequestMethod.POST } )
	public @ResponseBody ResponseSupport<Map<String, Object>> mapInfoListByInput(
			@RequestParam(value = "airportcode", required = false) String airportcode,
			@RequestParam(value = "mapname", required = false) String mapname,
			@RequestParam(value = "maptype", required = false) String maptype,
			@RequestParam(value = "page", required = true) Integer page,
			@RequestParam(value = "num", required = true) Integer num) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {
			Map<String, Object> map = mapinfoService.mapInfoListByInput(airportcode, mapname, maptype, num, (page - 1) * num);
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(map);
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		}
		return rsp;
	}
	/*
	 * 更新航图表内容，并更新机场表航图版本 
	 * 参数： 航图信息：mapinfo
	 * 		 上传文件 upfile
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/mapinfoupdate", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> UpdateMapInfo(@RequestParam(value = "mapinfo", required = true) String mapInfoParm,
			@RequestParam(value = "upfile", required = false) MultipartFile file ){
		ResponseSupport<String> ret = new ResponseSupport<String>();
		try {
			/*获取原始航图类型*/
			
			ObjectMapper mapper = new ObjectMapper();
			JsonNode rootNode = mapper.readTree(mapInfoParm);

			logger.debug("mapInfoParm:" + mapInfoParm);

			MapInfo mapinfo = new MapInfo();
			Date SysDate = new Date();
			String airportCode = rootNode.path("airportcode").textValue();
			String User = rootNode.path("updateuser").textValue();
			logger.debug("User:" + User);

			mapinfo.setMapCode(rootNode.path("mapcode").textValue());
			mapinfo.setAirportCode(airportCode);
			mapinfo.setMapName(rootNode.path("mapname").textValue());
			mapinfo.setMapType(rootNode.path("maptype").textValue());
			//mapinfo.setMapVersion(SysDate);
			mapinfo.setUpdateDate(SysDate);
			mapinfo.setUpdateUser(User);
			//mapinfo.setMapVersion(SysDate);

			/* 更新航图信息及机场航图版本 */
			mapinfoService.MapInfoUpdate(mapinfo, file);
			//Map<String, Object> map = new HashMap<String, Object>();

			/* 返回成功 */
			ret.setCode(RunServiceException.SUCCESS);
			return ret;
		} catch (IOException ex) {
			ex.printStackTrace();
			ret.setCode(RunServiceException.JSON_FORMAT_ERROR);
			ret.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			ret.setCode(ex.getCode());
			ret.setMessage(ex.getMessage());
		}
		
		
		return ret;
	}
	/*
	 * 插入指定页航图信息 参数： 航图信息：mapinfo
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/mapinfoinsert", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> InsertMapInfo(
			@RequestParam(value = "mapinfo", required = true) String mapInfoParm,
			@RequestParam(value = "upfile", required = true) MultipartFile file) {
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		ObjectMapper mapper = new ObjectMapper();
		MapInfo mapinfo = new MapInfo();

		try {        
			JsonNode rootNode = mapper.readTree(mapInfoParm);
			
			/* 解析接口 */
			mapinfo.setAirportCode(rootNode.path("airportcode").textValue());
			mapinfo.setMapName(rootNode.path("mapname").textValue());
			mapinfo.setMapType(rootNode.path("maptype").textValue());
			mapinfo.setFilePath(rootNode.path("filepath").textValue());
			mapinfo.setCreateUser(rootNode.path("createuser").textValue());

			/* 插入航图信息及机场航图版本 */
			mapinfoService.MapInfoInsert(mapinfo, file);
			rsp.setCode(RunServiceException.SUCCESS);
			return rsp;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(RunServiceException.JSON_FORMAT_ERROR);
			rsp.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
			return rsp;
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
	}
	/*
	 * 逻辑删除指定页航图信息 
	 * 参数： 机场代码：airportcode 
	 * 		 航图代码：mapcode
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/mapinfodelete", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> DeleteMapInfo(
			@RequestParam(value = "airportcode", required = true) String airportcode,
			@RequestParam(value = "mapcode", required = true) String mapcode,
			@RequestParam(value = "user", required = true) String user){
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		try {
			/* 删除指定航图 */
			mapinfoService.DeleteMapInfo(airportcode, mapcode,user);
			rsp.setCode(RunServiceException.SUCCESS);
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
		return rsp;
	}
	
	/*
	 * 逻辑删除指定页航图信息 ,不删除文件，因为区域图属于多个机场
	 * 参数： 机场代码：airportcode 
	 * 		 航图代码：mapcode
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/areainfodelete", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> DeleteAreaInfo(
			@RequestParam(value = "airportcode", required = true) String airportcode,
			@RequestParam(value = "mapcode", required = true) String mapcode,
			@RequestParam(value = "user", required = true) String user){
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		try {
			/* 删除指定航图 */
			mapinfoService.DeleteMapInfo(airportcode, mapcode,user, false);
			rsp.setCode(RunServiceException.SUCCESS);
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
		return rsp;
	}
	/*
	 * 新增区域图信息 
	 * 参数： 区域图信息：AreaMapInfo
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/instareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> InsertAireamap(
			@RequestParam(value = "mapinfo", required = true) String areaInfo,
			@RequestParam(value = "upfile", required = true) MultipartFile file) {
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		try {
			ObjectMapper mapper = new ObjectMapper();
			AreaMapInfo info = new AreaMapInfo();
			JsonNode rootNode = mapper.readTree(areaInfo);
			
			/* 解析接口 */
			info.setMapCode(rootNode.path("mapcode").textValue());
			info.setMapName(rootNode.path("mapname").textValue());
			info.setCreateUser(rootNode.path("createuser").textValue());
			
			areaServ.InsertAreaMap(info, file);
			rsp.setCode(RunServiceException.SUCCESS);
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(RunServiceException.JSON_FORMAT_ERROR);
			rsp.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
		}

		return rsp;
	}
	
	/*
	 * 查询所有记录数区域图 
	 * 参数： 区域图信息：AreaMapInfo
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/selectallareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<List<AreaMapInfo>> SelectAireamap() {
		ResponseSupport<List<AreaMapInfo>> rsp = new ResponseSupport<List<AreaMapInfo>>();
		try {
			List<AreaMapInfo> list = areaServ.SeleteAll();
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(list);
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		}
		return rsp;
	}
	/*
	 * 查询指定记录数区域图 
	 * 参数： 区域图信息：AreaMapInfo
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/selectareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> ListAireamap(
			@RequestParam(value = "mapcode", required = false) String mapCode,
			@RequestParam(value = "mapname", required = false) String mapName,
			@RequestParam(value = "num", required = true) int num,
			@RequestParam(value = "page", required = true) int page) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {
			Map<String, Object> map= areaServ.SeleteAreaMap(mapCode, mapName, num, (page - 1) * num);
					
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(map);
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		}
		return rsp;
	}

	/*
	 * 更新区域图信息 
	 * 参数： 区域图信息：AreaMapInfo
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/updateareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> UpdateAireamap(
			@RequestParam(value = "mapinfo", required = true) String areaInfo,
			@RequestParam(value = "upfile", required = false) MultipartFile file) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {
			ObjectMapper mapper = new ObjectMapper();
			AreaMapInfo info = new AreaMapInfo();
			JsonNode rootNode = mapper.readTree(areaInfo);
			
			/* 解析接口 */
			info.setMapCode(rootNode.path("mapcode").textValue());
			info.setMapName(rootNode.path("mapname").textValue());
			info.setUpdateUser(rootNode.path("updateuser").textValue());
			info.setFilePath(rootNode.path("filepath").textValue());
			areaServ.UpdateAreaMap(info, file);
					
			rsp.setCode(RunServiceException.SUCCESS);
			
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		} catch (IOException  ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
			rsp.setCode(RunServiceException.JSON_FORMAT_ERROR);
			rsp.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
		} 
		return rsp;
	}

	/*
	 * 删除区域图
	 * 参数： 航图编号：mapcode
	 * 		 用户: User
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/deleteareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> UpdateAireamap(
			@RequestParam(value = "mapcode", required = true) String mapCode,
			@RequestParam(value = "user", required = true) String User) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {			
			areaServ.DeleteAreaMap(mapCode, User);				
			rsp.setCode(RunServiceException.SUCCESS);
			
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		
		} 
		return rsp;
	}

	/*
	 * 增加区域图到航图表
	 * 参数： 
	 		航图编号：mapcode
	 		机场代码：airportcode
	 * 		 用户: User
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/addareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> AddAireamap(
			@RequestParam(value = "mapcode", required = true) String mapCode,
			@RequestParam(value = "airportcode", required = true) String airportCode,
			@RequestParam(value = "user", required = true) String User) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {			
			areaServ.addAreaMap(airportCode, mapCode, User);			
			rsp.setCode(RunServiceException.SUCCESS);
			
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		
		} 
		return rsp;
	}

	/*
	 * 查询航图列表里面的区域图
	 * 参数： 
	 		航图编号：mapcode
	 		机场代码：airportcode
	 * 		 用户: User
	 * 
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/getareamap", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> getAireamap(
			@RequestParam(value = "mapcode", required = false) String mapCode,
			@RequestParam(value = "airportcode", required = false) String airportCode,
			@RequestParam(value = "num", required = true) int num,
			@RequestParam(value = "page", required = true) int page) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {			
					
			Map<String, Object> map = mapinfoService.mapInfoAreaByInput(airportCode, mapCode, num,  (page - 1) * num);
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(map);
			
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		} 
		return rsp;
	}
	/*
	 * 手动生成压缩文件 
	 * 参数： 机场代码：airportcode
	 * 		用户：user
	 * 返回： 无
	 *
	 */
	@RequestMapping(value = "/genmapfile", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<Map<String, Object>> GenMapFile(
			@RequestParam(value = "airportcode", required = true) String airportCode,
			@RequestParam(value = "user", required = true) String User) {
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {			
			mapinfoService.generateMapZip(airportCode, User);			
			rsp.setCode(RunServiceException.SUCCESS);
			
		}catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		
		} 
		return rsp;
	}
	
    @ExceptionHandler(value=MultipartException.class)  
    public @ResponseBody ResponseSupport<String> allExceptionHandler(
    		Exception exception) throws Exception  
    {  
       	logger.debug(exception.getMessage());
       	ResponseSupport<String> responseSupport = new ResponseSupport<String>();
		responseSupport.setCode(1007);
		responseSupport.setMessage("文件大小不能超过20MB！");
		return responseSupport;
    }
}
